#!/bin/sh /etc/rc.common

USE_PROCD=1
START=99

CONF="spotifyd"
PROG="/usr/bin/spotifyd"

is_enabled() {
	local enabled
	config_get enabled "$1" "$2" "${3:-0}"
	if [ "$enabled" -eq "1" ]; then
		return 0
	else
		return 1
	fi
}

append_param_arg() {
	local value
	config_get value "$1" "$2" $3
	[ -n "$value" ] && procd_append_param command "--${2//_/-}" "$value"
}

append_param_bool() {
	is_enabled "$1" "$2" $3 && procd_append_param command "--${2//_/-}"
}

load_config_arg() {
	# device info
	append_param_arg "$1" "device_name"
	append_param_arg "$1" "device_type"

	# device control
	append_param_arg "$1" "backend"
	append_param_arg "$1" "control"
	append_param_arg "$1" "device"
	append_param_arg "$1" "mixer"

	# song control
	append_param_bool "$1" "autoplay"
	append_param_arg "$1" "audio_format"
	append_param_arg "$1" "bitrate"

	# volume control
	append_param_arg "$1" "initial_volume"
	append_param_bool "$1" "volume_normalisation"
	append_param_arg "$1" "normalisation_pregain"
	append_param_arg "$1" "volume_controller"

	# cache
	append_param_bool "$1" "no_audio_cache" "1"
	is_enabled "$1" "no_audio_cache" || append_param_arg "$1" "max_cache_size"

	# advanced
	append_param_arg "$1" "on_song_change_hook"
	append_param_arg "$1" "proxy"
	append_param_arg "$1" "zeroconf_port"
}

start_service() {
	config_load "$CONF"

	is_enabled "config" "enabled" || return 1

	procd_open_instance "$CONF"
	procd_set_param command "$PROG"
	procd_append_param command "--no-daemon"

	load_config_arg "config"

	# cache path
	local cache_path
	config_get cache_path "config" "cache_path" "/var/run/spotifyd"
	procd_append_param command "--cache-path" "$cache_path"
	mkdir -p "$cache_path"

	# credentials
	local username token
	config_get username "config" "username"
	config_get token "config" "token"
	if [ -n "$username" ] && [ -n "$token" ]; then
		mkdir -p "$cache_path/oauth"
		echo -n "{\"username\":\"$username\",\"auth_type\":1,\"auth_data\":\"$token\"}" > "$cache_path/oauth/credentials.json"
		chmod 0600 "$cache_path/oauth/credentials.json"
		mkdir -p "$cache_path/zeroconf"
		cp -fp "$cache_path/oauth/credentials.json" "$cache_path/zeroconf/"
	fi
	chown -R "spotifyd":"spotifyd" "$cache_path"

	procd_set_param user spotifyd
	procd_set_param group spotifyd

	procd_set_param respawn
	procd_set_param stdout 1
	procd_set_param stderr 1

	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "$CONF"
}
